package cz.hanis.android.geography.activities;

import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import cz.hanis.android.geography.GeographyApplication;
import cz.hanis.android.geography.R;
import cz.hanis.android.geography.WrongResourceException;
import cz.hanis.android.geography.adapters.CountryDetailPageAdapter;
import cz.hanis.android.geography.models.Country;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Window;


public class CountryDetailActivity extends SherlockActivity {

	public static final String COUNTRY_KEY = "country_key";
	private String countryName = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.country_detail);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);	
		
		
		
		int position = getIntent().getIntExtra(COUNTRY_KEY, 0);
		CountryDetailPageAdapter pageAdapter = 
				new CountryDetailPageAdapter(GeographyApplication.getInstance().getCountryTool().getTmpCountryForList(), this); 
		 ViewPager viewPager = (ViewPager) findViewById(R.id.vp_country_detail_viewpager);
         viewPager.setAdapter(pageAdapter);
         viewPager.setCurrentItem(position);
         final List<Country> countries = GeographyApplication.getInstance().getCountryTool().getTmpCountryForList();
         countryName = countries.get(position).getName();
         getSupportActionBar().setTitle(countryName);
         viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {			
			@Override
			public void onPageSelected(int arg0) {
				countryName = countries.get(arg0).getName();
				getSupportActionBar().setTitle(countryName);
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {				
				
			}
		});
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.country_detail_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Intent parentActivityIntent = new Intent(this, CountryListActivity.class);
//                parentActivityIntent.addFlags(
//                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
//                        Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(parentActivityIntent);
                finish();
                return true;
            case R.id.cdm_wiki:
            	
            	String prefix;
			try {
				prefix = GeographyApplication.getInstance().getStringValue("wiki_prefix");
			} catch (WrongResourceException e) {
				prefix = "en";
			}
            	String url = "http://" + prefix + ".wikipedia.org/wiki/" + countryName;
            	Intent intent = new Intent(Intent.ACTION_VIEW);
            	intent.setData(Uri.parse(url));
            	startActivity(intent);
            default:
                return super.onOptionsItemSelected(item);
        }
    } 	
    
    @Override
    public void onAttachedToWindow() {
    	super.onAttachedToWindow();
    	Window window = getWindow();
    	window.setFormat(PixelFormat.RGBA_8888);
    }    
	
}
